package passing.score.system.Controller;

import passing.score.system.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import passing.score.system.Model.User;
import passing.score.system.Service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class UserController {

    @Resource
    UserService userService;

    //主页登陆
    @RequestMapping(value = "/index.html")
    public  String index(HttpServletRequest request){
        request.getSession().setAttribute("status","");
        return "index";
    }

    //登陆验证
    @RequestMapping(value = "/login.html")
    public  String login(HttpServletRequest request,User user,String username){
        user.setUsername(username);
        User user1=userService.queryByUser(user);
        if(user1==null||!user1.getPassword().equals(user.getPassword())) {
            request.getSession().setAttribute("status", "登陆失败，请重试");
            return "index";
        }
        else{
            return "query";
        }
    }

    //注册界面
    @RequestMapping(value = "/register.html")
    public  String register(HttpServletRequest request){
        request.getSession().setAttribute("status","");
        return "register";
    }
    //处理注册
    @RequestMapping(value = "/newUser.html")
    public  String newUser(HttpServletRequest request,User user,String username){
        user.setUsername(username);
        User user1=userService.queryByUser(user);
        if(user1==null){
            userService.register(user);
            request.getSession().setAttribute("status","注册成功");
            return "index";
        }
        else{
            request.getSession().setAttribute("status","用户名已存在，请重新注册！");
            return "register";
        }
    }

    //查分页面
    @RequestMapping(value = "/query.html")
    public String query(){
        return "query";
    }

    //处理查询页面
    @RequestMapping(value = "/select.html")
    public String select(HttpServletRequest request,String score,String username){
        request.getSession().setAttribute("username",username);
        int temp=Integer.parseInt(score);
        if(temp>60)
            return "pass";
        else
            return "fail";
    }

    //录取界面
    @RequestMapping(value = "/pass.html")
    public String pass(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
            return "pass";
    }

    //没有录取界面
    @RequestMapping(value = "/fail.html")
    public String fail(HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        return "fail";
    }


    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
